One item picked by chance, every item equally likely. Give it a list - roll_one_of(frames) hands back one of the numbers in frames - or just write the choices out: roll_one_of(10, 20, 30). They can be any kind of thing: whole numbers, decimals, text.
roll_one_of(items, ...more) -> element| Name | Type |
|---|---|
items | list_or_element |
...more | element (any number of them, none included) |
element
Picks one item by chance, with every item as likely as any other. Give it a list, roll_one_of(faces), and it hands back one of the numbers in faces - or write the choices straight into the call, roll_one_of(10, 20, 30), two or more of them.
Either way it can be any kind of thing, and what you get back is that kind: whole numbers give you a whole number, decimals give you a decimal, text gives you text. Whole numbers and decimals can be mixed; you get a decimal then.
/// @type {list<int>}
var flicker_frames = [0, 2, 4];
frame = roll_one_of(flicker_frames);Picks one of three torch frames each step - but only frames 0, 2 and 4, never 1 or 3. That is the difference from roll_int(0, 4), which could land on any of the five: with roll_one_of you choose which values are allowed, and they do not have to sit in a row.
Write the choices out when they are few and never change: frame = roll_one_of(0, 2, 4); picks one of three torch frames, the same as the list above. Use a list when the choices can change while the game runs: list_add a new one and roll_one_of can land on it from then on.
With the values written out you need at least two, and they must be the same kind of thing (roll_one_of(1, "two") is refused). A list has to be made first, on a line of its own, as above. roll_one_of([1, 2, 3]) - a list written inside the call - is refused, and so is a fixed-size array: give it a list, or write the values out with no brackets.
roll_int range of one value.0 (or, for a list of decimals, text or yes-or-no values, the empty one of those: 0.0, the empty text, false) instead of stopping the game, and takes no draw. A list that can run out - a loot table, say - does not need a check before every pick; ask list_length when you need to tell "ran out" from "rolled a 0".roll_one_of is one roll like any other in the family. It is roll_int(0, last place) underneath, so it draws from the same single stream as roll_int, roll_real and roll_chance: two runs of the same ROM pick the same items in the same order, and roll_reseed restarts all of them together. See roll_int for why runs repeat by default, and for what a save file does and does not carry.
If you want to go through every item once, in a random order, that is list_shuffle - roll_one_of can pick the same item twice in a row.
One draw and one copy of the item. Measured at about 330 cycles a call, counting the loop around it - about 30 more than a roll_int on its own (a frame is 280,896 cycles). A list of text copies the whole text, so a very long string costs more.
One draw from the project-wide stream per call, the same stream roll_int uses (it is roll_int(0, last place) under the hood), so rolls repeat run to run and roll_reseed restarts them. With a list: a list with one item costs no draw at all, and an empty list has nothing to pick: it hands back 0 (or the empty kind of the list’s items - 0.0, false, an empty string) and takes no draw, rather than stopping the game. Only a list is accepted, not an array: roll_one_of([1, 2, 3]) is refused, because GBML cannot write a list into a call - write the choices straight into the call instead (roll_one_of(1, 2, 3)) or make the list first. With the choices written out you need at least two, and they must be the same kind of thing (whole numbers and decimals mix; the answer is then a decimal).